using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;

namespace Pronghorn.Core
{
	public abstract class SkinBase : IView, IViewDataContainer
	{
		private readonly ICollectionRenderer _collectionRenderer;
		private readonly IFileLinkRenderer _scriptRenderer;
		private readonly IFileLinkRenderer _cssRenderer;

		protected SkinBase(IProngHornSkinContext skinContext, 
			ICollectionRenderer collectionRenderer,
			IFileLinkRenderer scriptRenderer,
			IFileLinkRenderer cssRenderer)
		{
			_collectionRenderer = collectionRenderer;
			_scriptRenderer = scriptRenderer;
			_cssRenderer = cssRenderer;
			Model = skinContext;
		}

		protected IProngHornSkinContext Model
		{
			get; private set;
		}

		protected TempDataDictionary TempData
		{
			get; private set;
		}

		public ViewDataDictionary ViewData
		{
			get; set;
		}

		protected abstract void GenerateHtml(TextWriter writer);

		protected void RenderCss(TextWriter writer)
		{
			_cssRenderer.Render(Model.StyleSheets,writer);
		}

		protected void RenderScripts(TextWriter writer)
		{
			_scriptRenderer.Render(Model.Scripts, writer);
		}

		protected void RenderCollection(IList<IRenderable> renderables, TextWriter writer)
		{
			_collectionRenderer.Render(renderables,writer);
		}

		public void Render(ViewContext viewContext, TextWriter writer)
		{
			ViewData = viewContext.ViewData;
			TempData = viewContext.TempData;
			GenerateHtml(writer);
		}
	}
}